package com.atguigu.tingshu.order.strategy.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.strategy.TradeStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Package: com.atguigu.tingshu.order.strategy.impl
 *
 * @author xiongkunYang
 * @description: 策略接口具体实现类。订单项目类型为专辑
 * @since 2025/6/26 11:10
 */
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
@Slf4j
public class AlbumTradeStrategy implements TradeStrategy {

    private final UserFeignClient userFeignClient;
    private final AlbumFeignClient albumFeignClient;

    public AlbumTradeStrategy(UserFeignClient userFeignClient, AlbumFeignClient albumFeignClient) {
        this.userFeignClient = userFeignClient;
        this.albumFeignClient = albumFeignClient;
    }

    @Override
    public OrderInfoVo processTrade(Long userId, TradeVo tradeVo) {
        log.info("订单项目类型为专辑，处理专辑订单...");
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        // 远程调用判断用户是否购买过指定专辑
        Long albumId = tradeVo.getItemId();
        Result<Boolean> paidAlbumResult = this.userFeignClient.isPaidAlbum(albumId);
        if (ResultCodeEnum.FAIL.getCode().equals(paidAlbumResult.getCode())) {
            throw new GuiguException(ResultCodeEnum.REMOTE_ERROR);
        }
        if (paidAlbumResult.getData()) {
            throw new GuiguException(ResultCodeEnum.REPEAT_BUY_ERROR);
        }
        // 远程调用获取用户信息，判断用户是否为 vip
        Result<UserInfoVo> userInfoVoResult = this.userFeignClient.getUserInfoVoByUserId(userId);
        if (ResultCodeEnum.FAIL.getCode().equals(userInfoVoResult.getCode())) {
            throw new GuiguException(ResultCodeEnum.REMOTE_ERROR);
        }
        UserInfoVo userInfoVo = userInfoVoResult.getData();
        Assert.notNull(userInfoVo, "用户：{}不存在", userId);
        boolean isVip = userInfoVo.getIsVip().equals(1)
                && userInfoVo.getVipExpireTime() != null
                && userInfoVo.getVipExpireTime().after(new Date());

        // 远程调用获取专辑信息
        Result<AlbumInfo> albumInfoResult = this.albumFeignClient.getAlbumInfoById(albumId);
        if (ResultCodeEnum.FAIL.getCode().equals(albumInfoResult.getCode())) {
            throw new GuiguException(ResultCodeEnum.REMOTE_ERROR);
        }
        AlbumInfo albumInfo = albumInfoResult.getData();
        Assert.notNull(albumInfo, "专辑：{}不存在", albumId);
        // 处理订单相关价格
        BigDecimal originalAmount = albumInfo.getPrice();
        // 订单金额先设置为原始价格
        BigDecimal orderAmount = originalAmount;
        // 如果存在普通优惠
        BigDecimal discount = albumInfo.getDiscount();
        if (discount != null && discount.intValue() != -1) {
            orderAmount = originalAmount.multiply(discount).divide(BigDecimal.TEN, 2, RoundingMode.HALF_UP);
        }
        // 如果存在会员优惠且用户是 vip
        BigDecimal vipDiscount = albumInfo.getVipDiscount();
        if (vipDiscount != null && vipDiscount.intValue() != -1 && isVip) {
            orderAmount = originalAmount.multiply(vipDiscount).divide(BigDecimal.TEN, 2, RoundingMode.HALF_UP);
        }
        BigDecimal derateAmount = originalAmount.subtract(orderAmount);

        // 封装订单中商品明细列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(albumId);
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVoList.add(orderDetailVo);

        // 封装订单中优惠明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        if (derateAmount.compareTo(BigDecimal.ZERO) > 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("专辑优惠：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }

        this.setParam(orderInfoVo, originalAmount, orderAmount, derateAmount, orderDetailVoList, orderDerateVoList);

        return orderInfoVo;
    }

}
